package com.soulkun.smarthome.ui.main.scene

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.MarginPageTransformer
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainSceneBinding
import com.soulkun.smarthome.databinding.AppFragmentSplashLoginBinding
import com.soulkun.smarthome.databinding.AppFragmentSplashRegisterBinding
import com.soulkun.smarthome.ui.main.MainNavigationDataBindingFragment
import com.soulkun.smarthome.ui.main.scene.viewmodel.AppMainSceneViewModel
import soulkun.library.mvvm.component.BaseDataBindingFragment
import soulkun.library.utils.navigateSafe
import soulkun.library.utils.screen.ScreenUtils
import soulkun.library.utils.toPx

class AppMainSceneFragment : MainNavigationDataBindingFragment<AppFragmentMainSceneBinding>() {

    private val viewModel by lazy {
        ViewModelProvider(
            this,
            ViewModelProvider.AndroidViewModelFactory(mActivity.application)
        ).get(AppMainSceneViewModel::class.java)
    }

    private val viewPager2Adapter by lazy {
        object : FragmentStateAdapter(childFragmentManager, lifecycle) {
            override fun getItemCount(): Int {
                return viewModel.tabList.size
            }

            override fun createFragment(position: Int): Fragment {
                return if (position == 0) {
                    AppMainSceneAutoFragment()
                }else{
                    AppMainSceneOnekeyFragment()
                }
            }

        }
    }

    private val tabLayoutMediator by lazy {
        TabLayoutMediator(
            requireDataBinding().tlMainSceneLayoutTab,
            requireDataBinding().vp2MainSceneContent
        ) { tab, position ->
            tab.text = viewModel.tabList[position]
        }
    }

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_scene
    }

    override fun initView() {
        super.initView()
        ScreenUtils.setViewFitSystemWindows(requireDataBinding().root)
        requireDataBinding().vp2MainSceneContent.run {
            offscreenPageLimit = ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
            orientation = ViewPager2.ORIENTATION_HORIZONTAL
            adapter = viewPager2Adapter
        }
        tabLayoutMediator.attach()
    }

    override fun initData() {
        requireDataBinding().onClickListener = View.OnClickListener {
            when(it.id){
                R.id.view_main_scene_background_family -> {
                }
            }
        }
    }

    override fun initListener() {
    }

    override fun doOnCreate() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        tabLayoutMediator.detach()
    }

}